package com.example.armmvvm

import com.google.gson.Gson
import org.junit.Test

/**
 *  author : yanghaozhang
 *  date : 2022/11/15 14:55
 *  description :
 */
class ResTest {

    @Test
    fun testBussniss() {
        val testList = ArrayList<Bussniss>()
        testList.add(createFunc())
        testList.add(createFunc())
        testList.add(createFunc())
        val testA = createFunc().apply {
            data.funcName = "testA"
        }
        testA.children.add(createFunc())
        testA.children.add(createFunc().apply {
            data.isSubscribe = "1"
            data.funcName = "testA.B"
        })
        testA.children.add(createFunc())

        val testB = createFunc().apply {
            data.funcName = "testB"
        }
        testB.children.add(createFunc())
        testB.children.add(createFunc())
        val subC = createFunc().apply {
            data.isSubscribe = "0"
            data.funcName = "testB.Csub"
            data.funcType = "button"
        }
        val subCList = ArrayList<Bussniss>()
        subCList.add(subC)
        testB.children.add(createFunc().apply {
            data.isSubscribe = "1"
            data.funcName = "testB.C"
            children = subCList
        })

        testList.add(testA)
        testList.add(testB)

        Gson().toJson(testList)

        println(Gson().toJson(testList))
        println(Gson().toJson(getSubscribeFuncList(testList, true)))
        println(Gson().toJson(getSubscribeFuncList(testList, false)))
    }

    private fun createFunc(): Bussniss {
        val bussniss = Bussniss()
        bussniss.data.funcType = "func"
        bussniss.disabled = false
        return bussniss
    }

    /**
     * isFuncSubscribe:
     *      true-获取已订阅的层级列表
     *      false-获取未订阅的层级列表
     */
    private fun getSubscribeFuncList(list: List<Bussniss>, isFuncSubscribe: Boolean = true): ArrayList<Bussniss>? {
        var newList: ArrayList<Bussniss>? = null
        for (item in list) {
            if (isFunctionSubscribe(item, isFuncSubscribe)) {
                if (item.isFuncUnit()) {
                    if (newList == null) {
                        newList = ArrayList()
                    }
                    newList.add(item)
                } else {
                    val children = getSubscribeFuncList(item.children, isFuncSubscribe)
                    if (children != null) {
                        if (newList == null) {
                            newList = ArrayList()
                        }
                        newList.add(item.copy(children = children))
                    }
                }
            }
        }
        return newList
    }

    /**
     * 列表中是否有订阅的功能
     */
    private fun hasFunctionSubscribe(list: List<Bussniss>, isFuncSubscribe: Boolean = true): Boolean {
        for (item in list) {
            if (isFunctionSubscribe(item, isFuncSubscribe)) {
                return true
            }
        }
        return false
    }

    /**
     * 该功能是否已订阅/子集合中是否有已订阅功能
     */
    private fun isFunctionSubscribe(func: Bussniss, isFuncSubscribe: Boolean = true): Boolean {
        // 如果该func为文件夹
        if (!func.isFuncUnit()) {
            if (hasFunctionSubscribe(func.children, isFuncSubscribe)) {
                return true
            }
        }
        // 不是功能,是按钮
        else if (!func.data.isFunc()) {
            return false
        }
        // 当前功能被订阅
        else if (func.data.isFuncSubscribe() == isFuncSubscribe) {
            return true
        }
        return false
    }

    private fun Bussniss.isFuncUnit(): Boolean {
        // 子集合不为空且包含了功能:判断当前不是功能而是文件夹,判断子集合中是否有有效功能
        return !(children.isNotEmpty() && children.firstOrNull { it.data.isFunc() } != null)
    }
}